/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.presentationappearance;

import java.util.ArrayList;

import org.openaecfm.model.presentationappearance.TextureCoordinate;
import org.openaecfm.model.presentationappearance.TextureCoordinateGenerator;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class TextureCoordinateGeneratorIO extends
		AbstractEntityInstanceInputImpl<TextureCoordinateGenerator> implements
		IEntityInstanceOutput<TextureCoordinateGenerator> {

	private IEntityInstanceUtilOutput<TextureCoordinate> utilOutput;
	private IEntityInstanceUtilInput<TextureCoordinate> utilInput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<TextureCoordinate> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<TextureCoordinate> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(TextureCoordinateGenerator pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCTEXTURECOORDINATEGENERATOR", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set Mode
		ei.addParameter(new StringParameter(pojo.getMode(),
				StringParameter.UTF_8));

		// Set Parameter
		if (pojo.getParameter() == null || pojo.getParameter().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (Double d : pojo.getParameter()) {
				l.add(new RealParameter(d));
			}
			ei.addParameter(l);
		}

		return ei.toString();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			TextureCoordinateGenerator pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}

	@Override
	protected TextureCoordinateGenerator doCreateObject() {
		return new TextureCoordinateGenerator();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			TextureCoordinateGenerator pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Mode
		StringParameter mode = (StringParameter) sei.getParameter(1);
		pojo.setMode(mode.toUTF8());

		// Set Parameter
		Parameter p = sei.getParameter(2);
		if (p.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) p;
			if (pojo.getParameter() == null) {
				pojo.setParameter(new ArrayList<Double>());
			}
			for (Parameter pa : l) {
				RealParameter r = (RealParameter) pa;
				pojo.getParameter().add(r.toDouble());
			}
		}
	}
}
